//go:build windows
// +build windows

/*
@auther fenglei
*/
package xxsys

import (
	"context"
	"gitee.com/fenglei-l/ops-util/xxlog"
	"github.com/gookit/goutil/sysutil/cmdr"
	"time"
)

// 执行命令 sh -c
func ExecCmdSh(command ...string) (string, error) {
	c := cmdr.NewCmd("sh", "-c").WithArg(command...)
	xxlog.InfoUp366("执行语句:", c.Cmdline())
	out, err := c.CombinedOutput()
	if err != nil {
		xxlog.InfoUp366("执行语句返回err:", c.Cmdline(), out, err)
		if err.Error() != "exit status 1" {
			return out, err
		}
	}
	c.ResetArgs()
	return out, nil
}

// 用户执行命令 sh -c
func ExecCmdShUser(username string, args []string) (string, error) {
	return "", nil
}
func ExecCmdShTimeOut(timeout time.Duration, command ...string) (string, error) {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	c := cmdr.CmdWithCtx(ctx, "sh", "-c").WithArg(command...)
	xxlog.InfoUp366("执行语句:", c.Cmdline())
	out, err := c.CombinedOutput()
	if err != nil {
		xxlog.InfoUp366("执行语句返回err:", c.Cmdline(), out, err)
		if err.Error() != "exit status 1" {
			return out, err
		}
	}
	c.ResetArgs()
	return out, nil
}
